#!/usr/local/bin/python2.4
# -*- coding: UTF-8 -*-
# visualisering för klubb melt
# kristoffer josefsson 2005

from vj import *
from texturegl import *
from LSystemGL import *
from OpenGL.GL.EXT.texture_edge_clamp import *

import flickr
import urllib

from random import *
import string

class BridgeCubeRecreator(RecreatingCubeRecreator):

	def __init__(self,nrsides):
		RecreatingCubeRecreator.__init__(self,nrsides)

	def newside(self):
		bill = BridgeBillboard("bartender.png")		
		return bill

class FlickrCubeRecreator(RecreatingCubeRecreator):

	def __init__(self,nrsides):
		RecreatingCubeRecreator.__init__(self,nrsides)
		print("Searching for photos on flickr.com (slow)")
		self.search = flickr.photos_search(tags='fish', per_page=20)

	def newside(self):
	
		print("Choosing photo")
		url = self.search[randint(0,len(self.search)-1)].getURL(size='Medium', urlType='source')
		print("Getting url")
		file, mime = urllib.urlretrieve(url)
		print("Getting image")
		photo = Image.open(file)
		print("Building image")
		bill = BridgeBillboard(file)		
	
		bill.image = self.billt.image
		bill.make()   		
	
		return bill

class Bridge(Scene):
	def	__init__(self):
		Scene.__init__(self)
		
		self.title = "schmelting accident"
		self.fov = 40

	def	init(self, width, height):
	
		Scene.init(self, width, height)
		
		self.back = ImageBillboard(["manet.png","manet2.png"][randint(0,1)])
		self.krute = TTFBillboard("hotet", "vectroic.ttf",20 + randint(-10,10),1024)
		self.bill = RecreatingCube(BridgeCubeRecreator,1)
		
		self.lsgl = LSystemGL(uniform(0.6,2),uniform(-0.4,0.5))

		glMatrixMode(GL_PROJECTION)
		glLoadIdentity()					# reset	the	projection matrix
		gluPerspective(self.fov, float(width)/float(height), 0.1, 100.0)
						# calculate	aspect ratio of	window
		glMatrixMode(GL_MODELVIEW)

	def	display(self):
		Scene.display(self)
		
		for side in self.bill.sides:
			side.offset = self.rtri/100
		
		glTranslatef(-0.5,-0.5,-1)
		glColor4f(0.3,0.3,0.3,0.4)
	#	glDisable(GL_DEPTH_TEST)
		glEnable(GL_BLEND)
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA)
		self.back.draw()
		glPushMatrix()
		glTranslatef(0,0,0.1)
		self.bill.draw()
		glPopMatrix()
		glPushMatrix()
		glTranslatef(0,0,0.5+random()*sin(self.rtri)/3)
	#	self.krute.draw()
		glPopMatrix()
		
		glRotatef(self.rtri*5, 1,1,1)
		
	#	self.lsgl.draw()
	#	self.lsgl.update(sin(self.rtri/5))

		glutSwapBuffers()


	def	keyboard(self, key,	x, y):
		Scene.keyboard(self, key, x,y)
		
		if key in ['1','2','3','4','5','6']:
			self.bill.permutate(string.atoi(key))
		
		if key == 'r':
			self.init(640,480)
			self.bill.recreate()
		
		if key == 'o':
			for side in self.bill.sides:
				side.offset -= 0.01
		
		if key == 'O':
			for side in self.bill.sides:
				side.offset += 0.01

		if key == 'h':
			for side in self.bill.sides:
				side.height -= 0.1
		
		if key == 'H':
			for side in self.bill.sides:
				side.height += 0.1
			
		if key == 's':
			for side in self.bill.sides:
				side.softness -= 0.1
		
		if key == 'S':
			for side in self.bill.sides:
				side.softness += 0.1
if __name__=="__main__":
	
#	try:
#        	import psyco
#		psyco.full()
#   	except:
# 	     	print 'Psyco not found, ignoring it'


	s=Bridge()
	launchConsole()
	s.main(sys.argv)
	
